package com.situ.mall.controller.back;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/upload")
public class UploadController {
	
	@RequestMapping("/uploadImage")
	@ResponseBody
	public JSONResult uploadImage(MultipartFile file) {
		System.out.println("UploadController.uploadImage()");
		String fileName = file.getOriginalFilename();
		String name = UUID.randomUUID().toString().replace("-", "");
		String extension = FilenameUtils.getExtension(fileName);//	png
		//43b70df56b3e48ff959493b58da35574.png
		String newFileName = name + "." + extension;
		String filePath = "D:\\pic\\" + newFileName;
		try {
			file.transferTo(new File(filePath));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return JSONResult.ok("�ϴ��ɹ�" , newFileName);
	}
	
}
